package com.sunlands.zlcx.usercenter.controller;

import com.sunlands.zlcx.usercenter.constant.AppEnum;
import com.sunlands.zlcx.usercenter.domain.UsersDO;
import com.sunlands.zlcx.usercenter.service.UsersServiceImpl;
import com.sunlands.zlcx.usercenter.util.DozerMapper;
import com.sunlands.zlcx.usercenter.util.Version;
import com.sunlands.zlcx.usercenter.vo.UsersVO;
import org.springframework.security.access.AuthorizationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 封装了和安全有关的一些方法
 *
 * @author anjl
 */
public interface AuthSupport {
    String ANONYMOUS_USER = "anonymousUser";

    /**
     * 该方法不需要访问其他服务，可以在security中获取到用户的用户名和id
     *
     * @return UserInfoDTO
     */
    default Long currentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object object = authentication.getPrincipal();

        if (object instanceof String) {
            if (ANONYMOUS_USER.equals(object)) {
                return null;
            }
            String[] strings = ((String) object).split("-");
            if (strings.length == 2) {
                return Long.parseLong(strings[1]);
            } else {
                return 0L;
            }
        } else if (object instanceof UserDetails) {
            String temp = ((UserDetails) object).getUsername();
            String[] strings = temp.split("-");
            if (strings.length == 2) {
                return Long.parseLong(strings[1]);
            } else {
                throw new AuthorizationServiceException("用户名格式错误！" + "[" + strings[0] + "," + strings[1] + "]");
            }
        } else {
            try {
                throw new AuthorizationServiceException("没有获取到用户线信息！");
            } catch (AuthenticationException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 从当前认证过的环境中取出用户名, 进一步取出销售人员信息
     *
     * @param usersService usersService
     * @return SalesUserDTO
     */
    default UsersDO currentUsers(UsersServiceImpl usersService) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object object = authentication.getPrincipal();
        if (object instanceof String) {
            String[] strings = ((String) object).split("-");
            if (strings.length == 2) {
                 UsersVO usersVO = usersService.find(Long.parseLong(strings[1]));
                return DozerMapper.getInstance().map(usersVO, UsersDO.class);
            } else {
                return null;
            }
        } else {
            try {
                throw new AuthorizationServiceException("没有获取到用户线信息！");
            } catch (AuthenticationException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    //暂时先在放这里，如果后续比较多可以提成一个接口
    default String getApp(HttpServletRequest request) {
        String app = request.getHeader("app");
        app = Objects.nonNull(app) && AppEnum.postgraduate.name().equals(app) ?
                AppEnum.postgraduate.name() : AppEnum.tongzhuo.name();
        return app;
    }

    default String getVersion(HttpServletRequest request){
        return Version.getVersion(request);
    }
}
